import { getcellvalue } from './getdata'

const luckysheetArray = {
  transpose: function(getdata, useGetcellValue = true) {
    let arr = []
    if (getdata.length == 0) {
      return []
    }

    if (getdata[0].length == 0) {
      return []
    }

    for (let c = 0; c < getdata[0].length; c++) {
      let a = []
      for (let r = 0; r < getdata.length; r++) {
        let value = ''
        if (getdata[r] != null && getdata[r][c] != null) {
          if (useGetcellValue) {
            value = getcellvalue(r, c, getdata)
          } else {
            value = getdata[r][c]
          }
        }
        a.push(value)
      }
      arr.push(a)
    }

    return arr
  },
  minusClear: function(p, m) {
    if (
      m.row[0] > p.row[1] ||
      m.row[1] < p.row[0] ||
      m.column[0] > p.column[1] ||
      m.column[1] < p.column[0]
    ) {
      return null
    }

    if (
      m.row[0] == p.row[0] &&
      m.row[1] < p.row[1] &&
      m.column[0] > p.column[0] &&
      m.column[1] < p.column[1]
    ) {
      return []
    }

    let ret = [],
      range = { row: [], column: [] }

    let row1 = null,
      column1 = [p.column[0], p.column[1]]
    if (m.row[1] > p.row[0] && m.row[1] < p.row[1]) {
      row1 = [m.row[1] + 1, p.row[1]]
    } else if (m.row[0] > p.row[0] && m.row[0] < p.row[1]) {
      row1 = [p.row[0], m.row[0] - 1]
    }

    if (row1 != null) {
      ret.push({ row: row1, column: column1 })
    }

    let row2 = [p.row[0], p.row[1]],
      column2 = null
    if (m.column[1] > p.column[0] && m.column[1] < p.column[1]) {
      column2 = [m.column[1] + 1, p.column[1]]
    } else if (m.column[0] > p.column[0] && m.column[0] < p.column[1]) {
      column2 = [p.column[0], m.column[0] - 1]
    }

    if (column2 != null) {
      ret.push({ row: row2, column: column2 })
    }

    return ret
  },
}

export default luckysheetArray

/**
 * 平级数组转树
 * @param {Array} sNodes 需要转换的数组
 * @param {String} imtemId 子主键
 * @param {String} imtemParentId 父主键
 * @param {Object} OP 需要合并到子对象的值
 * @returns Tree 返回树形数组
 */
export function treeFormat(sNodes, imtemId, imtemParentId, OP) {
  if (!sNodes) return []
  if (sNodes.length < 2) {
    OP && Object.assign(sNodes[0], OP)
    return sNodes
  }
  var i, l
  var r = []
  var tmpMap = {}
  for (i = 0, l = sNodes.length; i < l; i++) {
    tmpMap[sNodes[i][imtemId]] = sNodes[i]
  }
  for (i = 0, l = sNodes.length; i < l; i++) {
    OP && Object.assign(sNodes[i], OP)
    var pId = sNodes[i][imtemParentId]
    var p = tmpMap[pId]
    if (p && sNodes[i][imtemId] !== pId) {
      var children = nodeChildren(p)
      if (!children) {
        children = nodeChildren(p, [])
      }
      children.push(sNodes[i])
    } else {
      r.push(sNodes[i])
    }
  }
  return r
}

// 获取节点子级  没有则设置为空数组
function nodeChildren(node, newChildren) {
  if (typeof newChildren !== 'undefined') {
    node.children = newChildren
  }
  return node.children
}
